import { Controller } from 'egg';

import { Result } from '../util';
import { BASIC_INFO_NAME } from '../../constants/user-basic-info';
import { uuidCommonRule, uuidContainAllRule } from './validate-rule';

class QuestionnaireController extends Controller {
  async search() {
    const { ctx } = this;

    ctx.validate({
      uuid: uuidCommonRule
    }, ctx.query);

    const { uuid } = ctx.query;

    const questionnaire = await ctx
      .service
      .questionnaire
      .findQuestionnaireByUuid(
        uuid
      );

    ctx.body = new Result({ questionnaire });
  }

  async show() {
    const { ctx } = this;

    ctx.validate({
      uuid: uuidCommonRule
    }, ctx.query);

    const { uuid } = ctx.query;

    const questionnaires = await ctx
      .service
      .questionnaire
      .findQuestionnaireByTemplateUuid(
        uuid
      );

    ctx.body = new Result({ questionnaires });
  }

  async create() {
    const { ctx } = this;

    ctx.validate({
      uuid: uuidCommonRule,
      name: {
        type: 'string',
        max: 100
      }
    }, ctx.request.body);

    const { uuid, name } = ctx.request.body;

    const result = await ctx.service.questionnaire.addQuestionnaire(
      uuid, name
    );

    if (result === null) {
      ctx.status = 422;
      ctx.body = new Result(undefined, '问卷已存在');
      return;
    }
    ctx.body = new Result({ questionnaires: result }, '问卷添加成功');
  }

  async updateOptionVisible() {
    const { ctx } = this;

    ctx.validate({
      uuid: uuidCommonRule,
      basicInformationName: {
        type: 'enum',
        values: BASIC_INFO_NAME,
      },
      value: {
        type: 'enum',
        values: [0, 1]
      },
    }, ctx.request.body);

    const {
      uuid,
      basicInformationName,
      value
    } = ctx.request.body;

    const optionSetting = await ctx
      .service
      .questionnaire
      .setOptionVisible(
        uuid,
        basicInformationName,
        value
      );

    if (optionSetting[0] < 1) {
      ctx.status = 422;
      ctx.body = new Result(undefined, '开启隐藏设置失败');
      return;
    }

    ctx.body = new Result({ optionSetting });
  }

  async delete() {
    const { ctx } = this;

    ctx.validate({
      uuid: uuidCommonRule
    }, ctx.request.body);

    const { uuid } = ctx.request.body;

    const result = await ctx.service.questionnaire.findMergeByUuid(uuid);
    if (!result) {
      ctx.status = 422;
      ctx.body = new Result(undefined, '删除失败，请先删除该问卷下的用户数据！');
      return;
    }

    const questionnaire = await ctx
      .service
      .questionnaire
      .deleteQuestionnaire(uuid);

    ctx.body = new Result({ questionnaire }, '删除成功！');
  }

  async updateSwitch() {
    const { ctx } = this;

    ctx.validate({
      uuid: uuidCommonRule,
      open: {
        type: 'enum',
        values: [0, 1]
      }
    }, ctx.request.body);

    const { uuid, open } = ctx.request.body;

    const changedSwitch = await this
      .ctx
      .service
      .questionnaire
      .switchHideSettings(
        uuid,
        open
      );

    if (changedSwitch[0] < 1) {
      ctx.status = 422;
      ctx.body = new Result(undefined, '开关设置失败');
      return;
    }

    ctx.body = new Result({ open });
  }

  async update() {
    const { ctx } = this;

    ctx.validate({
      uuid: uuidCommonRule,
      name: {
        type: 'string',
        max: 100
      },
    }, ctx.request.body);

    const { uuid, name } = ctx.request.body;

    const questionnaires = await ctx
      .service
      .questionnaire
      .updateQuestionnaire(
        uuid,
        name,
      );

    if (!questionnaires) {
      ctx.status = 422;
      ctx.body = new Result(undefined, '二级问卷名重复');
      return;
    }

    ctx.body = new Result({ questionnaires });
  }

  // FIXME: 不应该放在 questionnaire 中，应该放在 template
  async searchByQuestionnaireUuid() {
    const { ctx } = this;

    ctx.validate({
      uuid: {
        type: uuidCommonRule
      }
    }, ctx.query);

    const { uuid } = ctx.query;

    const template = await ctx
      .service
      .questionnaire
      .findTemplateByQuestionnaireUuid(
        uuid
      );

    ctx.body = new Result({ template });
  }

  async basicOptionList() {
    const { ctx } = this;

    ctx.validate({
      uuid: uuidCommonRule
    }, ctx.query);

    const { uuid } = ctx.query;

    const questionnaireDetailDataValue = await ctx
      .service
      .questionnaire
      .findQuestionnaireReferenceDetail(uuid);

    if (questionnaireDetailDataValue === undefined) {
      this.ctx.status = 500;
      this.ctx.body = new Result(undefined);
      return;
    }

    this.ctx.body = new Result(questionnaireDetailDataValue);
  }

  async searchManagerDetail() {
    const { ctx } = this;

    ctx.validate({
      uuid: uuidCommonRule
    }, ctx.query);

    const { uuid } = ctx.query;

    const questionnaireDetailDataValue = await ctx
      .service
      .questionnaire
      .searchDetail(uuid);

    if (questionnaireDetailDataValue === undefined) {
      ctx.status = 500;
      ctx.body = new Result(undefined);
      return;
    }

    ctx.body = new Result(questionnaireDetailDataValue);
  }

  async searchDetail() {
    const { ctx } = this;

    ctx.validate({
      uuid: uuidCommonRule
    }, ctx.query);

    const { uuid } = ctx.query;

    const questionnaireDetailDataValue = await ctx
      .service
      .questionnaire
      .searchDetail(uuid);

    if (questionnaireDetailDataValue === undefined) {
      this.ctx.status = 500;
      this.ctx.body = new Result(undefined);
      return;
    }

    if (
      questionnaireDetailDataValue
      && questionnaireDetailDataValue.open === false
    ) {
      this.ctx.body = new Result({ open: false }, '该问卷已关闭或不存在');
      return;
    }

    this.ctx.body = new Result(questionnaireDetailDataValue);
  }

  async searchForOption() {
    const { ctx } = this;

    ctx.validate({
      templateUuid: uuidContainAllRule,
      name: {
        type: 'string?',
        max: '60'
      },
      isAttachAll: {
        required: false,
        type: 'boolean'
      }
    }, ctx.query);

    const { templateUuid, name, isAttachAll } = ctx.query;

    const questionnaireOptionList = await ctx
      .service
      .questionnaire
      .findQuestionnaireOption(templateUuid, name, Boolean(isAttachAll));

    ctx.body = new Result(questionnaireOptionList);
  }
}

export default QuestionnaireController;
